package com.xintonghua.bussiness.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.ui.CustomerActivity;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dell on 2017/5/9.
 */

public class FriendAdapter extends BaseAdapter {
    private Context context;
    private List<Contant> list;

    public FriendAdapter(Context context, List<Contant> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Contant getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.friend_item_layout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Contant friend = list.get(position);

        if (position == getLetterFirstPosition(friend.getPinYinName())) {
            holder.tvLetter.setVisibility(View.VISIBLE);
            holder.tvLetter.setText(friend.getPinYinName());

        } else {
            holder.tvLetter.setVisibility(View.GONE);
        }
        holder.tv_up_to_dian.setText("" + friend.getPhone());
        holder.tvCustomerName.setText(friend.getNickname());
        holder.tvNickname.setText(friend.getNickname());
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                MyUtils.openActivity(context, CustomerActivity.class);
//            }
//        });
        holder.ivCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyUtils.callDial(context, "" + friend.getPhone());
            }
        });
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.tv_letter)
        TextView tvLetter;
        @BindView(R.id.tv_customer_name)
        TextView tvCustomerName;
        @BindView(R.id.rl_info)
        RelativeLayout rlInfo;
        @BindView(R.id.iv_call)
        ImageView ivCall;
        @BindView(R.id.tv_nickname)
        TextView tvNickname;
        @BindView(R.id.tv_up_to_dian)
        TextView tv_up_to_dian;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 获取首字母第一个出现的位置
     *
     * @param letter
     * @return
     */
    public int getLetterFirstPosition(String letter) {
        int pos = letter.charAt(0);
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getPinYinName();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == pos) {
                return i;
            }
        }
        return -1;
    }

    public List<Contant> getList() {
        return list;
    }

    public void setList(List<Contant> list) {
        this.list = list;
    }
}
